<?php
namespace app\admin\controller;
class Mechanism extends Common {


	//管理员表
	private $table_admin = "";

	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();
	}

	public function category_list(){

		$mechanism_category = db('mechanism_category')->where($where)->order('orders desc')->paginate(10);
		
		$this->assign('mechanism_category', $mechanism_category);
		$this->assign('page',$mechanism_category->render());
		return 	$this->fetch();
	}

	//添加机构分类
	public function add_category(){
		$id = input('id');

		$mechanism_category_result = db('mechanism_category')->where(array('id'=>$id))->find();

		if(request()->isAjax()){
			$name = input('name');
			$order = input('order');

			$data['name'] = $name;
			$data['orders'] = $order;

			if($id){
				$result = db('mechanism_category')->where(array('id'=>$id))->update($data);
			}else{
				$result = db('mechanism_category')->insert($data);
			}

			if(!$result)$this->error('操作失败');
			$this->success('操作成功',url('Mechanism/category_list'));
		}	

		$this->assign('mechanism_category_result',$mechanism_category_result);
		$this->assign('id',$id);
		return $this->fetch();
	}

	//机构
	public function lists(){
		$mid = input('mid');
		$pageParam = ['query' =>[]];
		$where = [];
		if(!empty($mid)){
			$where['m.mid'] = $mid;
			$pageParam['query']['mid'] = $mid;
		}

		$mechanism_result = db('mechanism')->alias('m')->join('mechanism_category mc','mc.id = m.mid')->where($where)->field('m.*,mc.name as category_name')->order('m.orders desc')->paginate(10, false, $pageParam)->each(function($item,$key){
			$item['img'] = IMG_PATH.$item['img'];
			return $item;
		});

		$mechanism_category = db('mechanism_category')->order('orders desc')->select();

		$this->assign('mechanism_category',$mechanism_category);	
		$this->assign('mechanism_result',$mechanism_result);
		$this->assign('page',$mechanism_result->render());
		$this->assign('mid',$mid);
		return $this->fetch();
	}

	//添加机构
	public function add(){
		$id = input('id');
		$mechanism_category = db('mechanism_category')->field('id,name')->select();
		$mechanism_result = db('mechanism')->where(array('id'=>$id))->find();

		if(request()->isAjax()){
			$name = input('name');
			$order = input('order');
			$mid = input('mid');
			$href = input('href');
			$path = input('path');
			$contact = input('contact');
			$content = input('content');
			$file = request()->file('picture');

			if ($file) {
				$info = $file->move(ROOT_PATH . 'public/static/uploads');
				if ($info) {
					$data['img'] = $info->getSaveName();
				}else {
					$this->success($file->getError());
				}
			}

			$data['name'] = $name;
			$data['orders'] = $order;
			$data['mid'] = $mid;
			$data['href'] = $href;
			$data['contact'] = $contact;
			$data['path'] = $path;
			$data['content'] = $content;

			if($id){
				$result = db('mechanism')->where(array('id'=>$id))->update($data);
			}else{
				$result = db('mechanism')->insert($data);
			}

			if(!$result)$this->error('操作失败');
			$this->success('操作成功',url('Mechanism/lists'));
		}

		$this->assign('mechanism_category',$mechanism_category);
		$this->assign('mechanism_result',$mechanism_result);
		$this->assign('id',$id);
		return $this->fetch();
	}
}